# #!/usr/bin/python
# # -*- coding: utf-8 -*-

import time

import sys
from PyQt5.Qt import QApplication, QGuiApplication, QClipboard
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QPlainTextEdit
from PyQt5.QtCore import QSize

class ExampleWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(440, 240))    
        self.setWindowTitle("PyQt5 Clipboard example") 

        # Add text field
        self.b = QPlainTextEdit(self)
        self.b.insertPlainText("Use your mouse to copy text to the clipboard.\nText can be copied from any application.\n")
        self.b.move(10,10)
        self.b.resize(400,200)

        # QApplication.clipboard().dataChanged.connect(self.clipboardChanged)
        # QApplication.clipboard().changed.connect(self.clipboardChanged)
        QApplication.clipboard().selectionChanged.connect(self.clipboardChanged)

    # Get the system clipboard contents
    def clipboardChanged(self):
        # self.close()
        print "xxxx"
        # text = QApplication.clipboard().text()
        # print(text)
        # self.b.insertPlainText(text + '\n')

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    mainWin = ExampleWindow()
    mainWin.show()
    import random
    dd = "%d"%(random.randint(100,999))
    print dd
    QApplication.clipboard().setText(dd)
    print "===="
    sys.exit( app.exec_() )


# def main():
#     c = QGuiApplication.clipboard()
#     print c
#     c.set_text("dddd")
#     print(text)

# main()


# from PyQt5 import QtCore, QtGui, Qt
# app = QtGui.QGuiApplication([])

# # data = QtCore.QMimeData()
# # url = QtCore.QUrl.fromLocalFile('c:\\foo.file')
# # data.setUrls([url])

# # app.clipboard().setMimeData(data)
# # t = app.clipboard().text()
# # print t
# data = QtCore.QMimeData()
# data.setText("======")
# c = app.clipboard()
# c.setMimeData(data)
# # app.clipboard().setText("eelllll")
# # app.setText("eeeeeaaaeee")
# event = QtCore.QEvent(QtCore.QEvent.Clipboard)
# app.sendEvent(c, event)
# # time.sleep(2)
# c.setText("Clipboard Text", mode=c.Clipboard)
